package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Product;
import com.sjlh.centurylianhua.entity.SupplierProduct;
import com.sjlh.centurylianhua.entity.UserRole;
import com.sjlh.centurylianhua.mapper.ProductMapper;
import com.sjlh.centurylianhua.mapper.SupplierProductMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RequestMapping("/supplierProduct")
@RestController
public class SupplierProductController {
    @Resource
    private SupplierProductMapper supplierProductMapper;

    @Resource
    private ProductMapper productMapper;

    @RequestMapping("/showList")
    public ResultData getProductBySupplierId(@RequestBody SupplierProduct supplierProduct){
        QueryWrapper<SupplierProduct> supplierMapper = new QueryWrapper<>();
        supplierMapper.eq(supplierProduct.getSupplierId() != null,"supplierId",supplierProduct.getSupplierId());
        List<SupplierProduct> supplierList = supplierProductMapper.selectList(supplierMapper);
        List<Integer> ids= new ArrayList<>();
        for (SupplierProduct list:supplierList) {
            ids.add(list.getProductId());
        }
        if(ids.size()==0){
            return new ResultData(true,20000,"成功",new ArrayList<>(),0L);
        }else {
            // 封装条件查询构造器
            QueryWrapper<Product> productWrapper = new QueryWrapper<Product>();
            productWrapper.in("product.id", ids);
            //追加多表连接的sql语句
            productWrapper.apply("product.typeId = producttype.id");
            // 数据为空时返回的所有数据
            List<Product> list = productMapper.getInfo(productWrapper);
            long count = productMapper.getInfo(productWrapper).size();
            return new ResultData(true, 20000, "成功", list, count);
        }
    }

    /**
     * 批量分配商品
     * @param sc
     * @return
     */
    @RequestMapping("/addsp")
    public Object addsp(@RequestBody SearchCon sc) {
        List<SupplierProduct> list = new ArrayList<SupplierProduct>();
        QueryWrapper<SupplierProduct> wra = new QueryWrapper<SupplierProduct>();
        wra.eq(sc.getTypeName() != null, "supplierId", sc.getTypeName());
        List<SupplierProduct> listsupplier = supplierProductMapper.selectList(wra);
        if (listsupplier != null && listsupplier.size() != 0) {//判断查询回来的穿梭框里面有没有内容
            UpdateWrapper<SupplierProduct> updateWrapper = new UpdateWrapper<SupplierProduct>();//先清除掉他有的商品
            updateWrapper.eq(sc.getTypeName() != null, "supplierId", sc.getTypeName());
            supplierProductMapper.delete(updateWrapper);//移除所有商品
        }

        boolean r = true;
            for (String s : sc.getNamelist()) {//执行添加
                SupplierProduct sup = new SupplierProduct(null, sc.getTypeName(), Integer.parseInt(s));
                Integer rs = supplierProductMapper.insert(sup);
                if(rs!=1){
                    r=false;
                }
            }
            if (r) {
                return new ResultData(true, 20000, "分配成功!", r, null);
            } else {
                return new ResultData(true, 20000, "分配失败!", r, null);
            }
    }

    /**
     * 批量移出商品
     * @param sc
     * @return
     */
    @RequestMapping("/delsp")
    public Object delsp(@RequestBody SearchCon sc) {
        boolean r = true;
        for (String s : sc.getNamelist()) {//循环获取商品id
            SupplierProduct sup = new SupplierProduct(null, sc.getTypeName(), Integer.parseInt(s));
            UpdateWrapper<SupplierProduct> sp = new UpdateWrapper<>();
            sp.eq("supplierId",sc.getTypeName());
            sp.eq("productId",Integer.parseInt(s));
            Integer rs = supplierProductMapper.delete(sp);
            sp.clear();
            if(rs!=1){
                r=false;
            }
        }
        if (r) {
            return new ResultData(true, 20000, "移出成功!", r, null);
        } else {
            return new ResultData(true, 20000, "移出失败!", r, null);
        }
    }

}
